home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / monkeysphere / transitions / 0.23 next >
Encoding:
Text File  |  2011-03-24  |  7.7 KB  |  215 lines

  1. #!/bin/bash
  2.  
  3. # This is a post-install script for monkeysphere, to transition an old
  4. # (<0.23) setup to the new (>=0.23) setup.
  5.  
  6. # You should be able to run this script after any version >= 0.23 is
  7. # installed.  This script should be well-behaved, even if it is run
  8. # repeatedly.
  9.  
  10. # Written by
  11. # Jameson Rollins <jrollins@finestructure.net>
  12. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  13. #
  14. # Copyright 2009, released under the GPL, version 3 or later
  15.  
  16. # NOTE: the reverse operation (downgrading) is not directly supported,
  17. # and MAY LOCK YOU OUT OF YOUR SYSTEM, depending on how you have
  18. # configured the monkeysphere!
  19.  
  20. # any unexpected errors should cause this script to bail:
  21. set -e
  22.  
  23. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  24. export SYSSHAREDIR
  25. . "${SYSSHAREDIR}/defaultenv"
  26.  
  27. MADATADIR="${SYSDATADIR}/authentication"
  28. MHDATADIR="${SYSDATADIR}/host"
  29.  
  30. STASHDIR="${SYSDATADIR}/backup-from-0.23-transition"
  31.  
  32.  
  33. log() {
  34.     printf "$@" >&2
  35. }
  36.  
  37. # FIXME: implement this function better.  here, we only care about
  38. # dots, *and* about reversing the regexification of them.
  39. gpg_unescape_and_unregex() {
  40.     sed  's/\\x5c\././g'
  41. }
  42.  
  43.  
  44. is_domain_name() {
  45.     printf "%s" "$1" | egrep -q '^[[:alnum:]][[:alnum:]-.]*[[:alnum:]]$'
  46. }
  47.  
  48.  
  49. # move the old server conf file to be the authentication conf file
  50. if [ -f "$SYSCONFIGDIR"/monkeysphere-server.conf -a \
  51.     ! -f "$SYSCONFIGDIR"/monkeysphere-authentication.conf ] ; then
  52.     mv "$SYSCONFIGDIR"/monkeysphere-server.conf "$SYSCONFIGDIR"/monkeysphere-authentication.conf
  53. fi
  54.  
  55. # run the authentication setup (this is also the first chance to bail
  56. # if 0.23 is not fully-installed, because m-a did not exist before
  57. # 0.23)
  58. monkeysphere-authentication setup
  59.  
  60. # before 0.23, the old gnupg-host data directory used to contain the
  61. # trust core and the system's ssh host key.  
  62.  
  63. if [ -d "$SYSDATADIR"/gnupg-host ] ; then
  64.  
  65. ### transfer identity certifiers, if they don't already exist in the
  66. ### current setup:
  67.  
  68.     if monkeysphere-authentication list-identity-certifiers | \
  69.     grep -q '^[A-F0-9]{40}:$' ; then
  70.     log 'There are already certifiers in the new system!\nNot transferring any certifiers.\n'
  71.     else
  72.     # get the old host keygrip (don't know why there would be more
  73.     # than one, but we'll transfer all tsigs made by any key that
  74.     # had been given ultimate ownertrust):
  75.     for authgrip in $(GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --export-ownertrust | \
  76.         grep ':6:$' | \
  77.         sed -r 's/^[A-F0-9]{24}([A-F0-9]{16}):6:$/\1/') ; do
  78.         
  79.     # we're assuming that old id certifiers were only added by old
  80.     # versions of m-s c+, which added certifiers by ltsigning
  81.     # entire keys.
  82.         
  83.     # so we'll walk the list of tsigs from the old host key, and
  84.     # add those keys as certifiers to the new system.
  85.  
  86.         # FIXME: if an admin has run "m-s add-id-certifier $foo"
  87.         # multiple times for the same $foo, we'll only transfer
  88.         # one of those certifications (even if later
  89.         # certifications had different parameters).
  90.         
  91.         GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --fingerprint --with-colons --fixed-list-mode --check-sigs | \
  92.         cut -f 1,2,5,8,9,10 -d: | \
  93.         egrep '^(fpr:::::|sig:!:'"$authgrip"':[[:digit:]]+ [[:digit:]]+:)' | \
  94.         while IFS=: read -r type validity grip trustparams trustdomain fpr ; do
  95.         case $type in
  96.             'fpr') # this is a new key
  97.             keyfpr=$fpr
  98.             ;;
  99.             'sig') # deal with all trust signatures, including
  100.                # regexes if present.
  101.             if [ "$keyfpr" ] ; then
  102.                 trustdepth=${trustparams%% *}
  103.                 trustlevel=${trustparams##* }
  104.                 if [ "$trustlevel" -ge 120 ] ; then
  105.                 truststring=full
  106.                 elif [ "$trustlevel" -ge 60 ] ; then
  107.                 truststring=marginal
  108.                 else
  109.                 # trust levels below marginal are ignored.
  110.                 continue
  111.                 fi
  112.  
  113.                 finaldomain=
  114.                 if [ "$trustdomain" ] ; then
  115.                 # FIXME: deal with translating
  116.                 # $trustdomain back to a domain.
  117.                 if [ printf "%s" "$trustdomain" | egrep -q '^<\[\^>\]\+\[@\.\][^>]+>\$$' ] ; then
  118.                     dpart=$(printf "%s" "$trustdomain" | sed -r 's/^<\[\^>\]\+\[@\.\]([^>]+)>\$$/\1/' | gpg_unescape_and_unregex)
  119.                     if [ is_domain_name "$dpart" ]; then
  120.                     finaldomain="--domain $dpart"
  121.                     else
  122.                     log "Does not seem to be a domain name (%s), not adding certifier\n" "$dpart"
  123.                     continue
  124.                     fi
  125.                 else
  126.                     log "Does not seem to be a standard gpg domain-based tsig (%s), not adding certifier\n" "$trustdomain"
  127.                     continue
  128.                 fi
  129.                 fi
  130.  
  131.                 CERTKEY=$(mktemp ${TMPDIR:-/tmp}/mstransition.XXXXXXXX)
  132.                 log "Adding identity certifier with fingerprint %s\n" "$keyfpr"
  133.                 GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --export "0x$keyfpr" --export-options export-clean >"$CERTKEY"
  134.                 MONKEYSPHERE_PROMPT=false monkeysphere-authentication add-identity-certifier $finaldomain --trust "$truststring" --depth "$trustdepth" "$CERTKEY"
  135.                 rm -f "$CERTKEY"
  136.                 # clear the fingerprint so that we don't
  137.                 # make additional tsigs on it if more uids
  138.                 # are present:
  139.                 keyfpr=
  140.             fi
  141.             ;;
  142.         esac
  143.         done
  144.     done
  145.     fi
  146.  
  147. ### transfer host key information (if present) into the new spot
  148.     
  149.     if [ -d "${MHDATADIR}" ] ; then
  150.     log "Not transferring host key info because host directory already exists.\n"
  151.     else
  152.     if [ -s "$SYSDATADIR"/ssh_host_rsa_key ] || \
  153.         GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --with-colons --list-secret-keys | grep -q '^sec:' ; then
  154.         
  155.         FPR=$(GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --with-colons --fixed-list-mode --list-secret-keys --fingerprint | awk -F: '/^fpr:/{ print $10 }' )
  156.         
  157.     # create host home
  158.         mkdir -p $(dirname "$MHDATADIR")
  159.         NEWDATADIR=$(mktemp -d "${MHDATADIR}.XXXXXX")
  160.         chmod 0700 "${NEWDATADIR}"
  161.         
  162.         log "importing host key from old monkeysphere installation\n"
  163.  
  164. # export from the pubring as well as the that new (non-expired)
  165. # self-sigs are available, otherwise the secret key import may fail
  166.  
  167. # FIXME: turns out the secret key import fails anyway, stupidly :(
  168.  
  169. # FIXME: if all self-sigs are expired, then the secret key import may
  170. # fail anyway. How should we deal with that?
  171.         
  172.         if (GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --export-secret-keys && \
  173.         GNUPGHOME="$SYSDATADIR"/gnupg-host gpg --quiet --no-tty --no-permission-warning --export "$FPR") | \
  174.         GNUPGHOME="$NEWDATADIR" gpg --quiet --no-tty --import ; then
  175.         : we are in good shape!
  176.         else
  177.         if ! GNUPGHOME="$NEWDATADIR" gpg --quiet --no-tty --list-secret-key >/dev/null ; then
  178.             log "The old host key (%s) was not imported properly.\n" "$FPR"
  179.             exit 1
  180.         fi
  181.         fi
  182.         
  183.         # if we get here cleanly, then we're OK to move forward:
  184.         mv "$NEWDATADIR" "$MHDATADIR"
  185.  
  186.         monkeysphere-host update-gpg-pub-file
  187.     else
  188.         log "No host key found in old monkeysphere install; not importing any host key.\n"
  189.     fi
  190.     fi
  191.  
  192.  
  193. ### get rid of this old stuff, since we've transferred it all:
  194.  
  195.     mkdir -p "$STASHDIR"
  196.     chmod 0700 "$STASHDIR"
  197.     mv "${SYSDATADIR}/gnupg-host" "$STASHDIR"/gnupg-host.$(date '+%F_%T%z')
  198. fi
  199.  
  200.  
  201. # There is nothing in the old authentication directory that we should
  202. # need to keep around, but it is not unreasonable to transfer keys to
  203. # the new authentication keyring.
  204. if [ -d "${SYSDATADIR}/gnupg-authentication" ] ; then
  205.  
  206.     GNUPGHOME="${SYSDATADIR}/gnupg-authentication" \
  207.     gpg --quiet --no-tty --no-permission-warning --export 2>/dev/null | \
  208.     monkeysphere-authentication gpg-cmd --import 2>/dev/null || \
  209.     log "No OpenPGP certificates imported into monkeysphere-authentication trust sphere.\n"
  210.  
  211.     mkdir -p "$STASHDIR"
  212.     chmod 0700 "$STASHDIR"
  213.     mv "${SYSDATADIR}/gnupg-authentication" "$STASHDIR"/gnupg-authentication.$(date '+%F_%T%z')
  214. fi
  215.